package com.wwp.wkb.base;

import com.wwp.wkb.R;
import com.wwp.wkb.base.impl.LoadingSwipeRefresh;

import io.reactivex.disposables.Disposable;

public abstract class BaseNetActivity extends BaseActivity implements Request, Loading {
    public Loading loading;
    public Disposable disposable;

    @Override
    protected void setUpView() {
        createLoading();
    }

    protected void createLoading() {
        loading = new LoadingSwipeRefresh(this, getLoadingViewId(), getRefreshViewId());
    }

    protected int getLoadingViewId() {
        return R.id.loadingView;
    }

    protected int getRefreshViewId() {
        return R.id.swipeRefreshLayout;
    }

    //第一次网络请求，这个是特殊的，因为显示的遮罩的loading
    @Override
    public void load() {
        if (loading != null) {
            loading.onLoading();
        }
        request();
    }

    @Override
    public void request() {

    }

    @Override
    public void refresh() {
    }

    @Override
    public void cancel() {
        if (disposable != null) {
            disposable.dispose();
        }
    }

    @Override
    public void setRequest(Request request) {
        if (loading != null) {
            loading.setRequest(this);
        }
    }

    @Override
    public void onLoading() {
        if (loading != null) {
            loading.onLoading();
        }
    }

    @Override
    public void onSucceed() {
        if (loading != null) {
            loading.onSucceed();
        }
    }

    @Override
    public void onFailure(String errCode, String errMsg) {
        if (loading != null) {
            loading.onFailure(errCode, errMsg);
        }
    }

    @Override
    public void onEmpty(String errMsg) {
        if (loading != null) {
            loading.onEmpty(errMsg);
        }
    }

    @Override
    public void onError(int layoutId, int imgId, String errMsg) {
        if (loading != null) {
            loading.onError(layoutId, imgId, errMsg);
        }
    }
}
